package com.bd.scm.module.utils;

import com.alibaba.fastjson.JSONObject;
import com.bd.scm.module.qywx.thread.TokenThread;
import com.bd.scm.module.qywx.thread.WxTokenThread;
import com.bd.scm.module.utils.httpClient.HttpRequestUtil;
import com.bd.scm.module.utils.wx.WXAuthUtil;
import com.bd.wx.core.util.WeiXinParamesUtil;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class WxJSUtil {

    /**
     * 微信公众号
     *
     * @param url
     * @return
     */
    public static Map<String, String> jsAPisign(String url) {

        /**
         *
         */
        String appId = WXAuthUtil.APPID;

        /**
         * 判断ticketJson是否为空  为空重新获取
         */
        JSONObject  ticketJson = WxTokenThread.ticketJson;

        /**
         *
         */
        if (ticketJson!=null){
            System.out.println(ticketJson.toString());

        }else
        {
            System.out.println("ticketJson is null");
        }

        /**
         *ticket
         */
        String jsapi_ticket = ticketJson.getString("ticket");

        Map<String, String> ret = new HashMap<String, String>();
        //这里的jsapi_ticket是获取的jsapi_ticket。

        /**
         *
         */
        String nonce_str = create_nonce_str();

        /**
         *
         */
        String timestamp = create_timestamp();

        String string1;

        String signature = "";

        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                "&noncestr=" + nonce_str +
                "&timestamp=" + timestamp +
                "&url=" + url;

        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
            System.out.println("crypt=" + crypt.toString());
            System.out.println("string1=" + string1);
            System.out.println("signature=" + signature);

        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        ret.put("appId",appId);
        return ret;
    }

    /**
     * 企业微信
     *
     * @param url
     * @return
     */
    public static Map<String, String> jsQyAPisign(String url) {

        /**
         *
         */
        String corpid = WeiXinParamesUtil.getProperty("corpId");

        String agentid = WeiXinParamesUtil.getProperty("agentId");

        /**
         * 判断ticketJson是否为空  为空重新获取
         */
        JSONObject  ticketJson = TokenThread.ticketJson;

        /**
         *
         */
        if (ticketJson!=null){
            System.out.println(ticketJson.toString());

        }else
        {
            System.out.println("ticketJson is null");
        }

        /**
         *ticket
         */
        String jsapi_ticket = ticketJson.getString("ticket");

        Map<String, String> ret = new HashMap<String, String>();
        //这里的jsapi_ticket是获取的jsapi_ticket。

        /**
         *
         */
        String nonce_str = create_nonce_str();

        /**
         *
         */
        String timestamp = create_timestamp();

        String string1;

        String signature = "";

        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
            "&noncestr=" + nonce_str +
            "&timestamp=" + timestamp +
            "&url=" + url;

        try {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
            System.out.println("crypt=" + crypt.toString());
            System.out.println("string1=" + string1);
            System.out.println("signature=" + signature);

        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);
        ret.put("corpid",corpid);
        ret.put("agentid",agentid);
        return ret;
    }


    /**
     * @param hash
     * @return
     */
    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    /**
     * 必填，生成签名的随机串
     *
     * @return
     */
    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }


    /**
     * 必填，生成签名的时间戳
     *
     * @return
     */
    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }


    /**微信公众号
     * @param access_token
     * @return
     */
    public static JSONObject getTicketTool(String access_token) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";
//    	System.out.println(HttpRequestUtil.httpRequest(url, "GET", ""));
        System.out.println("wechat-获取getTicketTool:"+url);
        return HttpRequestUtil.httpRequest(url, "GET", "");
    }

    /**企业微信
     * @param access_token
     * @return
     */
    public static JSONObject getQyTicketTool(String access_token) {
//        String url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token="+access_token+"&type=agent_config";
//    	System.out.println(HttpRequestUtil.httpRequest(url, "GET", ""));
        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="+access_token;
        System.out.println("Qywechat-获取getTicketTool:"+url);
        return HttpRequestUtil.httpRequest(url, "GET", "");
    }
}
